import {
  personList,
  cesiumGridList,
  cesiumSubGridList,
  cmsbasicsGetByResType,
  portGetPort,
  didianGetList,
  propertyCfnGetByType,
  presetFieldGetListByPropertyId,
  companyGetList,
  classifyTreeGetList,
  classifyTreeGetListByCompanyId,
  sysGetUserListByUserName,
  knowledgeListNodes,
  groupGetYunweiRole
} from "@/api";
import throttle from "lodash/throttle";
import uniqBy from "lodash/uniqBy";
import store from "@/store";
import { Notify } from "@/utils/notification";
import { translateDataToTree } from "@/utils/parentSon";
export const selectObj = (field = "name", title = "人员名称", options = {}) => {
  return {
    type: "select",
    field,
    title,
    tableVisible: true,
    props: {
      filterable: true,
      remote: true,
      placeholder: "请选择",
      clearable: true
    },
    options: [],
    on: {},
    ...options
  };
};

export const cascaderObj = (field = "test", title = "demo", options = {}) => {
  return {
    type: "cascader",
    field,
    title,
    tableVisible: true,
    props: {
      filterable: true,
      showAllLevels: false,
      remote: true,
      emitPath: false,
      placeholder: "请选择",
      clearable: true,
      multiple: true,
      options: []
    },
    on: {},
    ...options
  };
};

export const methodGenerator = ({
  fn,
  queryParam = {},
  querykey = "name",
  callback
}) => {
  return throttle((qs = "") => {
    const query = {
      //   pageNo: 1,
      //   pageSize: 20,
      ...queryParam
    };
    query[querykey] = qs;
    fn(query).then(res => {
      callback(res);
    });
  });
};

export const personSearch = (
  field = "name",
  title = "人员名称",
  options = {}
) => {
  const select = selectObj(field, title, options);
  const method = methodGenerator({
    fn: personList,
    queryParam: {
      gender: 0,
      position: ""
    },
    querykey: "name",
    callback: res => {
      select.options = res.data.dataList.map(i => {
        i.label = i.name;
        i.value = i.id;
        return i;
      });
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

export const ownerMethod = select => {
  return methodGenerator({
    fn: personList,
    queryParam: {
      gender: 0,
      position: "房东"
    },
    querykey: "name",
    callback: res => {
      select.options = res.data.dataList.map(i => {
        i.label = i.name;
        i.value = i.id;
        return i;
      });
    }
  });
};

export const personSearchLabel = (
  field = "name",
  title = "人员名称",
  options = {}
) => {
  const select = selectObj(field, title, options);
  const method = methodGenerator({
    fn: personList,
    queryParam: {
      gender: 0,
      position: ""
    },
    querykey: "name",
    callback: res => {
      select.options = res.data.dataList.map(i => {
        i.label = i.name;
        i.value = i.name;
        return i;
      });
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

export const gridSearch = (
  field = "parentGrid",
  title = "网格名称",
  options = {}
) => {
  const select = selectObj(field, title, options);
  const method = methodGenerator({
    fn: cesiumGridList,
    queryParam: {
      gender: 0,
      name: "",
      label: "",
      district: "",
      street: ""
    },
    querykey: "label",
    callback: res => {
      select.options = res.data.dataList.map(i => {
        i.label = i.label;
        i.value = i.id;
        return i;
      });
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

export const gridSearchLabel = (
  field = "parentGrid",
  title = "网格名称",
  options = {}
) => {
  const select = selectObj(field, title, options);
  const method = methodGenerator({
    fn: cesiumGridList,
    queryParam: {
      gender: 0,
      name: "",
      label: "",
      district: "",
      street: ""
    },
    querykey: "label",
    callback: res => {
      select.options = res.data.dataList.map(i => {
        i.label = i.label;
        i.value = i.label;
        return i;
      });
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

export const unitSearch = (
  field = "subGrid",
  title = "单元名称",
  options = {}
) => {
  const select = selectObj(field, title, options);
  const method = methodGenerator({
    fn: cesiumSubGridList,
    queryParam: {
      name: "",
      parentGridName: "",
      doctorName: "",
      policeName: "",
      socialPersonName: ""
    },
    querykey: "name",
    callback: res => {
      select.options = res.data.dataList.map(i => {
        i.label = i.label;
        i.value = i.id;
        return i;
      });
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

export const getMapGetPortsByip = (
  field = "ports",
  title = "关联端口",
  options = {},
  presets = {}
) => {
  const select = selectObj(field, title, options);
  const method = methodGenerator({
    fn: portGetPort,
    queryParam: {
      ...presets
    },
    querykey: "ipaddress",
    callback: res => {
      select.options = res.data.map(i => {
        i.label = i;
        i.value = i;
        return i;
      });
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

export const getMainResource = (
  field = "resId",
  title = "关联资源",
  options = {},
  presets = {}
) => {
  const select = selectObj(field, title, options);
  const method = methodGenerator({
    fn: cmsbasicsGetByResType,
    queryParam: {
      page: 1,
      size: 1000,
      ...presets
    },
    querykey: "customName",
    callback: res => {
      let data = res.data.data;
      store.state.topoNode.data = data;
      select.options = data.map(i => {
        i.label = i.customName;
        i.value = i.resId;
        return i;
      });
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

export const getSite = (
  field = "didianId",
  title = "地点列表",
  options = {},
  presets = {}
) => {
  const select = selectObj(field, title, options);
  const method = methodGenerator({
    fn: didianGetList,
    queryParam: {
      page: 1,
      size: 1000,
      ...presets
    },
    querykey: "didianName",
    callback: res => {
      let data = res.data.list;
      select.options = data.map(i => {
        i.label = i.didianName;
        i.value = i.id;
        return i;
      });
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

export const getProperty = (
  field = "name",
  title = "资产分类",
  options = {},
  presets = {}
) => {
  const select = selectObj(field, title, options);
  const method = methodGenerator({
    fn: propertyCfnGetByType,
    queryParam: {
      //   page: 1,
      //   size: 1000,
      ...presets
    },
    querykey: "name",
    callback: res => {
      let data = res.data;
      //   if (data && data.length) {
      select.options = data.map(i => {
        i.label = i.propertyName;
        i.value = i.id;
        return i;
      });
      //   } else {
      //     Notify.normal({
      //       message: `当前单位没有资产分类!`,
      //     });
      //     select.options = [];
      //     return false;
      //   }
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

export const getPresetField = (
  field = "name",
  title = "资产类别自定义",
  options = {},
  presets = {}
) => {
  const select = selectObj(field, title, options);
  const method = methodGenerator({
    fn: presetFieldGetListByPropertyId,
    queryParam: {
      //   page: 1,
      //   size: 1000,
      ...presets
    },
    querykey: "name",
    callback: res => {
      let data = res.data;
      select.options = data.map(i => {
        i.label = i.fieldName;
        i.value = i.id;
        return i;
      });
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

export const getCompanyList = (
  field = "name",
  title = "单位",
  options = {},
  presets = {}
) => {
  const select = selectObj(field, title, options);
  const method = methodGenerator({
    fn: companyGetList,
    queryParam: {
      page: 1,
      size: 1000,
      ...presets
    },
    querykey: "name",
    callback: res => {
      let data = res.data;
      select.options = data.map(i => {
        i.label = i.name;
        i.value = i.companyId;
        return i;
      });
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

export const getTypeList = (
  field = "name",
  title = "类型",
  options = {},
  presets = {}
) => {
  const select = cascaderObj(field, title, options);
  const method = methodGenerator({
    fn: classifyTreeGetList,
    queryParam: {
      ...presets
    },
    querykey: "name",
    callback: res => {
      let data = res.data;

      data.forEach(element => {
        Object.assign(element, {
          id: element.treeId,
          value: element.treeId,
          label: element.treeName
        });
        if (element.parentId) {
          element.parent = element.parentId.toString();
        } else {
          element.parent = element.parentId;
        }
      });
      //   console.log('sessionStorage.getItem("classTreeList")');
      //   console.log(JSON.parse(sessionStorage.getItem("classTreeList")));
      if (sessionStorage.getItem("classTreeList")) {
        select.props.options = JSON.parse(
          sessionStorage.getItem("classTreeList")
        );
      } else {
        select.props.options = translateDataToTree(data);
      }
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

export const getTypeListByCompanyId = (
  field = "name",
  title = "类型",
  options = {
    clearable: true
  },
  presets = {}
) => {
  const select = cascaderObj(field, title, options);
  const method = methodGenerator({
    fn: classifyTreeGetListByCompanyId,
    queryParam: {
      ...presets
    },
    querykey: "name",
    callback: res => {
      let data = res.data;
      data.forEach(element => {
        Object.assign(element, {
          id: element.treeId,
          value: element.treeId,
          label: element.treeName
        });
        if (element.parentId) {
          element.parent = element.parentId.toString();
        } else {
          element.parent = element.parentId;
        }
      });
      if (options.isHaveOptions) {
        return;
      }
      select.props.options = translateDataToTree(data);
      if (select.value.length) {
      }
      //   select.value = [
      //     select.props.options[0]?.id,
      //     select.props.options[0]?.children[0]?.id?.children[0]?.id
      //   ];
      //   console.log("select.props.options");
      //   console.log(data);
      //   console.log(select.props.options);
      //   console.log(value);
    }
  });
  select.props.remoteMethod = method;
  //   select.props.checkStrictly = true;
  console.log("select-----");
  console.log(select);
  select.value = [];

  select.on.focus = () => {
    method("");
  };
  return select;
};

export const getTypeListByWiki = (
  field = "name",
  title = "类型",
  options = {},
  presets = {}
) => {
  const select = cascaderObj(field, title, options);
  const method = methodGenerator({
    fn: knowledgeListNodes,
    queryParam: {
      ...presets
    },
    querykey: "name",
    callback: res => {
      let data = res.data;
      console.log("data------------");
      console.log(data);
      data.forEach(element => {
        Object.assign(element, {
          id: element.id,
          value: element.id,
          label: element.categoryName
        });
        if (element.parentId) {
          element.parent = element.parentId.toString();
        } else {
          element.parent = element.parentId;
        }
      });
      select.props.options = translateDataToTree(data);
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

export const getNiuzhuanList = (
  field = "userName",
  title = "处理人",
  options = {},
  presets = {}
) => {
  const select = cascaderObj(field, title, options);
  const method = methodGenerator({
    fn: sysGetUserListByUserName,
    queryParam: {
      ...presets
    },
    // querykey: "userName",
    callback: res => {
      let data = res.data;
      select.props.options = data.map(i => {
        i.label = i.userName;
        i.value = i.userName;
        return i;
      });
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};

/**
 * @name 获取所有运维角色列表
 */
export const getYunweiRole = (
  field = "roleId",
  title = "运维角色",
  options = {},
  presets = {}
) => {
  const select = selectObj(field, title, options);
  const method = methodGenerator({
    fn: groupGetYunweiRole,
    queryParam: {
      ...presets
    },
    querykey: "roleName",
    callback: res => {
      let data = res.data;
      select.options = data.map(i => {
        i.label = i.roleName;
        i.value = i.id;
        return i;
      });
    }
  });
  select.props.remoteMethod = method;
  select.on.focus = () => {
    method("");
  };
  return select;
};
